# python 基础

# 1. 变量，字符串，编码
print("hello python")
print(1 + 2 + 3)
a = 123
b = a
a = "abc"
print(a)
print(b)
print(ord("o"))
print("\n\n")
print(chr(73), chr(108) + chr(111) + chr(118) + chr(101), chr(85))
print("ABC".encode('ascii'))
print("中文".encode('utf-8'))
print("中文".encode('gb2312'))
print(b'\xe4\xb8\xad\xe6\x96\x87'.decode('utf-8'))
print(len("abc"))
print(len("中文"))
print(len("中文".encode('utf-8')))
print("hi %s, you have $%d" % ('ljy', 10000))
print("h1,{0},成绩为{1:.1f}".format("ljy", 80.266666))

print("\n\n")
# 2. 使用list和tuple
# 有序集合list(其实就是java中数组)
dogs = ['ab', 'ac', 'ad', 'ae']
print(len(dogs))
print(dogs)
dogs.append("af")  # 追加元素
print(dogs)
dogs.insert(0, 'aa')  # 插入元素
print(dogs)
print(dogs[0])
print(dogs[1])
print(dogs.pop())  # 删除末尾元素
print(dogs.pop(0))  # 删除指定索引元素
print(dogs)
dogs[0] = 'abbbbbb'  # 修改元素
print(dogs)
# 有序列表,元组,tuple,初始化后不能修改
# 因为tuple不可变，所以代码更安全。如果可能，能用tuple代替list就尽量用tuple。
cats = ('qa', 'qb', 'qc')
print(cats)
t = (11, 12, ['aaa', 'bbb'])
print(t)
t[2][0] = 'ccc'
t[2][1] = 'ddd'
print(t)

# 3. 条件判断
i = 3
if i <= 1:
    print("11111")
elif i < 2:
    print("22222222")
elif i == 3:
    print("3333")
else:
    print("44444")

s = 'abc'
# 只要x是非零数值、非空字符串、非空list等，就判断为True，否则为False。
if s:
    print('True')
    print("hhhhh")  # python用缩进控制语句块的是否结束,不像java用{ ...},这一点很重要
else:
    print("False")

# s = input('age=')
# age = int(s)  # 因为input()返回的数据类型是str，str不能直接和整数比较，必须先把str转换成整数
# if age > 30:
#     print("90前")
# else:
#     print("90后")

# 4. 循环
arr = ['111', '222', '333']
for i in arr:
    print(i)
print(list(range(5)))
sum = 0
for i in range(10):
    sum += i
print(sum)
n = 100
while n > 0:
    n -= 1
    if n % 2 == 0:
        continue
    if n == 50:
        break
    sum = sum + n
print(sum)

# 5. 使用dict 和 set,
# dict 字典,就是java中的map
d = {'k1': 111, 'k2': 112, 'k3': 113}
print(d['k3'])
d['k3'] = 114
print(d['k3'])
# set 和java中set类似,存储一组值,且值不能重复
s = {1, 2, 3, 3, 4}
print(s)
s.add(4)
s.add(5)
print(s)
s2 = set(['11', '11', '11', '12', '13'])
print(s2)
s2.remove("11")
print(s2)
s11 = {1, 2, 3}
s12 = {2, 3, 4}
print(s11 & s12)
print(s11 | s12)

a = [1, 3, 2, 6, 5, 4]
a.sort()
print(a)
s = 'abc'
s.replace('a', "A")
print(s)
s2=s.replace('a', "A")
print(s2)
